package com.ad.pet.app.address;

import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ad.pet.R;
import com.ad.pet.models.remote.address.Address;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * q地址适配器，注意要点地址的东西必须弄清楚语言格式
 * 一般都承认英文，国语简体只适合大陆地区，港澳台坡都是繁体字中文
 * Created by SEELE on 2018/6/25.
 */

public class AddressAdapter extends BaseQuickAdapter<Address, BaseViewHolder> {
    OnClickEditLinstener mOnClickEditLinstener;
    Address mAddress;

    public AddressAdapter(@Nullable List<Address> data) {
        super(R.layout.item_address_list_layout, data);
    }
    public AddressAdapter(@Nullable List<Address> data,Address address) {
        super(R.layout.item_address_list_layout, data);
        mAddress = address;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final Address item) {
        TextView tvTitle = helper.getView(R.id.tv_title);
        TextView tv_name = helper.getView(R.id.tv_name);
        TextView tv_phone = helper.getView(R.id.tv_phone);
        TextView tv_label = helper.getView(R.id.tv_label);
        TextView tv_default = helper.getView(R.id.tv_default);
        TextView tv_address_detail = helper.getView(R.id.tv_address_detail);
        LinearLayout linearOut = helper.getView(R.id.linear_out);
        LinearLayout tvPost = helper.getView(R.id.linear_edit);
        String address = item.getAddress();

        if (item.getIs_default()==1||helper.getLayoutPosition()==0){
            tvTitle.setText("常用收货地址");
            tvTitle.setVisibility(View.VISIBLE);
            tv_default.setVisibility(View.VISIBLE);
        }else if (item.getIs_default()!=1&&helper.getLayoutPosition()==1){
            tvTitle.setText("其他收货地址");
            tvTitle.setVisibility(View.VISIBLE);
            tv_default.setVisibility(View.GONE);
        }else {
            tv_default.setVisibility(View.GONE);
            tvTitle.setVisibility(View.GONE);
        }
        String label = "";
        switch (item.getLabel()){
            case 1:
                label = "家";
                break;
            case 2:
                label = "公司";

                break;
            case 3:
                label = "学校";
                break;
        }
        tv_label.setText(label);
        tv_name.setText(item.getConsigner());
        if (!TextUtils.isEmpty(address)) {
            address = address + " ";
        }
        String city = "";
        if (!TextUtils.isEmpty(item.getCity_name())) {
            city = item.getCity_name() + " ";
        }
        String state = "";
        if (!TextUtils.isEmpty(item.getProvince_name())) {
            state = item.getProvince_name() + " ";
        }
        String d = "";
        if (!TextUtils.isEmpty(item.getDistrict_name())) {
            d = item.getDistrict_name() + " ";
        }
        address =  state + city+d +address     ;
        tv_address_detail.setText(Html.fromHtml(address));


        tv_phone.setText(item.getMobile());

      linearOut.setOnLongClickListener(new View.OnLongClickListener() {
          @Override
          public boolean onLongClick(View v) {
              mOnClickEditLinstener.onDelete(helper.getLayoutPosition());
              return true;
          }
      });

        tvPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnClickEditLinstener.onEdit(helper.getLayoutPosition());
            }
        });
    }

    public void setOnClickEditLinstener(OnClickEditLinstener onClickEditLinstener) {
            mOnClickEditLinstener = onClickEditLinstener;
    }

     interface OnClickEditLinstener {
        void onEdit(int position);
        void onDelete(int position);
        void onDefult(int position);
    }
}
